<template>
  <Form @submit="onSubmit" @failed="onFailed">
    <CellGroup title="" inset>
      <Field v-model.trim="form.keyword" label="搜索关键字" placeholder="默认使用电影/剧集名称进行搜索" clearable autocomplete="no" />

      <Field label="推送下载">
        <template #input>
          <Switch v-model="form.pushToClient" size="20" />
        </template>
      </Field>
      <!-- <Field label="名称过滤">
        <template #input>
          <Switch v-model="form.filterTitle" size="20" />
        </template>
      </Field>
      <Field label="年份过滤">
        <template #input>
          <Switch v-model="form.filterYear" size="20" />
        </template>
      </Field>
      <Field label="媒体类型过滤">
        <template #input>
          <Switch v-model="form.filterMediaType" size="20" />
        </template>
      </Field> -->
    </CellGroup>
    <CellGroup title="启用过滤" inset>
      <Cell title="名称过滤" label="种子标题是否包含电影/剧集名称">
        <template #right-icon>
          <Switch v-model="form.filterTitle" size="20" />
        </template>
      </Cell>
      <Cell title="年份过滤" label="种子标题是否包含电影/剧集年份">
        <template #right-icon>
          <Switch v-model="form.filterYear" size="20" />
        </template>
      </Cell>
      <Cell title="媒体类型过滤" label="根据RSS信息猜测是否为电影/剧集">
        <template #right-icon>
          <Switch v-model="form.filterMediaType" size="20" />
        </template>
      </Cell>
    </CellGroup>
    <CheckboxGroup v-model="form.sites">
      <CellGroup title="排除站点" inset>
        <Cell v-for="(site, index) in siteOptions" :key="site.id" :title="site.name" :label="site.address">
          <template #right-icon>
            <Checkbox :name="site.id" />
          </template>
        </Cell>
      </CellGroup>
    </CheckboxGroup>
    <div style="margin: 16px;">
      <Button round block type="primary" native-type="submit" loading-type="spinner" :loading="submitting">
        保 存
      </Button>
    </div>
  </Form>
</template>
<script setup>
import { Space, Form, Field, Switch, Checkbox, CheckboxGroup, Cell, CellGroup, Button, Popup, Picker, Notify } from 'vant';
import { reactive, ref, onBeforeUpdate } from 'vue';

import BaseApi from '@/api/api.js'
import store from '@/store'

const siteOptions = store.state.siteOptions.filter(site => site.rss);

const module = 'sub'

const emits = defineEmits(['close'])

const form = reactive({
  sites: [],
  keyword: '',
  filterTitle: true,
  filterYear: true,
  filterMediaType: true,
  pushToClient: true,
});


const submitting = ref(false);
const onSubmit = (values) => {
  // console.log('submit', form);

  submitting.value = true;
  BaseApi.update(module, { id: subId, search: JSON.stringify(form) }).then(response => {
    if (response.data.code === 1) {

      planForm();

      Notify({ type: 'success', message: response.data.message ? response.data.message : '保存成功' });

      emits('close');
    } else {
      Notify({ type: 'danger', message: `保存失败：${response.data.message}` });
    }
  }).catch((error) => {
    Notify({ type: 'danger', message: `保存失败：${error.message}` });
  }).then(() => {
    submitting.value = false;
  })
};

const onFailed = (errorInfo) => {
  // console.log('failed', errorInfo);
  // Notify({ type: 'danger', message: `推送失败：${errorInfo.errors.message}` });
};

const planForm = () => {
  form.sites = []; //siteOptions.map(site => site.id);
  form.keyword = '';
  form.filterTitle = true;
  form.filterYear = true;
  form.filterMediaType = true;
  form.pushToClient = true;
}

let subId = undefined;
const initModel = (item) => {
  // console.log(item);
  planForm();
  if (item) {
    subId = item.id;
    BaseApi.find(module, subId).then(response => {
      if (response.data.code === 1) {
        let search = response.data.data.search;
        if (search) {
          search = JSON.parse(search);
          form.sites = search.sites || []; //siteOptions.map(site => site.id);
          form.keyword = search.keyword;
          form.filterTitle = search.filterTitle;
          form.filterYear = search.filterYear;
          form.filterMediaType = search.filterMediaType;
          form.pushToClient = search.pushToClient;
        }
      }
    });
  }
}


defineExpose({
  initModel
})


  // init functions

</script>